const axios = require('axios');

(async () => {
  console.log('🏆 最终勋章显示测试');
  
  try {
    // 1. 登录主用户
    const loginResponse = await axios.post('http://localhost:3000/api/auth/login', {
      email: 'admin@campus.com',
      password: 'admin123'
    });
    
    const authToken = loginResponse.data.token;
    const userId = loginResponse.data.user.id;
    console.log(`✅ 主用户登录成功: ID=${userId}`);
    
    // 2. 获取所有勋章
    const allBadgesResponse = await axios.get('http://localhost:3000/api/badges', {
      headers: { Authorization: `Bearer ${authToken}` }
    });
    
    console.log(`📊 勋章总数: ${allBadgesResponse.data.badges.length}`);
    
    // 3. 分析勋章状态
    let ownedCount = 0;
    let availableCount = 0;
    
    allBadgesResponse.data.badges.forEach(badge => {
      if (badge.status === 'minted' && badge.user_id === userId) {
        ownedCount++;
      } else if (badge.status === 'available') {
        availableCount++;
      }
    });
    
    console.log(`🎖️ 已获得勋章: ${ownedCount}个`);
    console.log(`🔓 未获得勋章: ${availableCount}个`);
    
    // 4. 测试筛选功能
    const ownedBadgesResponse = await axios.get('http://localhost:3000/api/badges?filter=owned', {
      headers: { Authorization: `Bearer ${authToken}` }
    });
    
    console.log(`🔍 筛选"我的勋章": ${ownedBadgesResponse.data.badges.length}个`);
    
    // 5. 显示前几个勋章的详细状态
    console.log('\n📋 勋章状态示例:');
    
    // 显示已获得的勋章
    const ownedBadges = allBadgesResponse.data.badges.filter(badge => 
      badge.status === 'minted' && badge.user_id === userId
    ).slice(0, 2);
    
    console.log('✅ 已获得的勋章:');
    ownedBadges.forEach((badge, index) => {
      console.log(`  ${index + 1}. ${badge.badge_name}`);
      console.log(`     - 状态: ${badge.status}`);
      console.log(`     - 前端应显示: 已获得 ✅`);
    });
    
    // 显示未获得的勋章
    const availableBadges = allBadgesResponse.data.badges.filter(badge => 
      badge.status === 'available'
    ).slice(0, 2);
    
    console.log('\n❌ 未获得的勋章:');
    availableBadges.forEach((badge, index) => {
      console.log(`  ${index + 1}. ${badge.badge_name}`);
      console.log(`     - 状态: ${badge.status}`);
      console.log(`     - 前端应显示: 未获得 ❌`);
    });
    
    // 6. 测试新用户
    console.log('\n👤 测试新用户视角...');
    
    try {
      const newUser = {
        username: `testuser${Date.now()}`,
        email: `test${Date.now()}@test.com`,
        password: 'test123'
      };
      
      const registerResponse = await axios.post('http://localhost:3000/api/auth/register', newUser);
      const newAuthToken = registerResponse.data.token;
      
      const newUserBadgesResponse = await axios.get('http://localhost:3000/api/badges?filter=owned', {
        headers: { Authorization: `Bearer ${newAuthToken}` }
      });
      
      console.log(`🆕 新用户拥有勋章: ${newUserBadgesResponse.data.badges.length}个 (应该为0)`);
      
      if (newUserBadgesResponse.data.badges.length === 0) {
        console.log('✅ 新用户正确显示：无已获得勋章');
      } else {
        console.log('❌ 新用户错误显示了勋章');
      }
      
    } catch (newUserError) {
      console.log('⚠️ 新用户测试跳过');
    }
    
    // 7. 总结
    console.log('\n🎯 勋章显示修复验证:');
    console.log(`✅ 总勋章数量: ${allBadgesResponse.data.badges.length} (应该等于活动数)`);
    console.log(`✅ 已获得勋章: ${ownedCount}个`);
    console.log(`✅ 未获得勋章: ${availableCount}个`);
    console.log(`✅ 筛选功能: 正确返回${ownedBadgesResponse.data.badges.length}个已获得勋章`);
    
    console.log('\n🎨 前端应该显示:');
    console.log('- 已获得的勋章：彩色显示，状态为"已获得"');
    console.log('- 未获得的勋章：灰色显示，状态为"未获得"，带锁定图标');
    console.log('- 筛选"我的勋章"时：只显示已获得的勋章');
    console.log('- 筛选"全部勋章"时：显示所有勋章，区分获得状态');
    
  } catch (error) {
    console.error('❌ 测试失败:', error.message);
    if (error.response) {
      console.error('详细错误:', error.response.data);
    }
  }
})(); 